<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="author" content="Jxm">
    <title></title>
    <style>
     ul,li{
         margin: 0;
         padding: 0;
         list-style: none;
     }       
     #box{
         width: 520px;
         height: 280px;
         margin: 100px auto;
         position: relative;
     }
     #box .list li{
         position: absolute;
         top: 0;
         left: 0;
         /*display: none;*/
     }
     #box .list li.current{
         display: block;
     }
     #box .count{
         position: absolute;
         right: 10px;
         bottom: 10px;
     }
     #box .count li{
         float: left;
         width: 20px;
         height: 20px;
         border-radius: 50%;
         background-color: #f60;
         text-align: center;
         line-height: 20px;
         margin-left: 10px;
         color: #fff;
         opacity: 0.8;
         cursor: pointer;
     }
     #box .count li.current{
         background-color: orangered;
         opacity: 1;
     }
    </style>
</head>

<body>
    <div id="box">
        <ul class="list">
            <li><img src="img/1.jpg" alt=""></li>
            <li><img src="img/2.jpg" alt=""></li>
            <li><img src="img/3.jpg" alt=""></li>
            <li><img src="img/4.jpg" alt=""></li>
            <li><img src="img/5.jpg" alt=""></li>
        </ul>
        <ul class="count">
            <li class="current">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
            <li>5</li>
        </ul>
    </div>
</body>

</html>

<script>

    var box = document.querySelector('#box');
    var aLi = document.querySelectorAll('.list li');
    var aNum = document.querySelectorAll('.count li');
    // 记录当前显示的图片的索引
    var current = 0;
    // 保存定时器
    var timer = null;

    // 自动的
    timer = setInterval(autoPlay, 1000);
    function autoPlay() {
        current++;
        if (current >= aLi.length) {
            current = 0;
        }
        show(current);
    }

    // 手动
    for(var i=0;i<aNum.length;i++){
        // 存储下标
        aNum[i].index=i;
        aNum[i].onmouseover=function(){
            show(this.index);
        }
    }

    // 处理图片的显示和隐藏
    function show(index){
        // 手动改变时 需要更新current的值
        current=index;
        // 当前图片显示时 那么其他的图片隐藏
        for(var i=0;i<aLi.length;i++){
            aLi[i].style.display='none';
            aNum[i].className='';
        }
        // 让当前图片显示
        aLi[index].style.display='block';
        // 改变图片对应数字的状态
        aNum[index].className='current';
    }

    // 手动的时候 当鼠标移入box容器的时候 让autoPlay停止 移出继续播放
    box.onmouseover=function(){
        clearInterval(timer);
    }
    box.onmouseout=function(){
        timer=setInterval(autoPlay,1000);
    }

</script>